﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 29/03/2014
 * Time: 04:46 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using System.Data.OleDb;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
    /// <summary>
    /// Description of paisdaimpl.
    /// </summary>
    public class paisdaimpl : Gestión_UTN.da.paisda
    {
        public paisdaimpl()
        {
        }

        public bool SavePais(string nombre)
        {
            bool ingresado = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "INSERT INTO pai_paises(pai_nombre)" +
                                "VALUES(@pai_nombre)";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand insertar = new OleDbCommand(sql, conex, trans);

                    insertar.Parameters.AddWithValue("@pai_nombre", nombre);

                    insertar.ExecuteNonQuery();

                    trans.Commit();

                    ingresado = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                ingresado = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return ingresado;
        }

        public bool DeletePais(string nombre)
        {
            bool eliminado = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "DELETE FROM pai_paises WHERE pai_nombre = @pai_nombre";

                    conex.Open();

                    trans = conex.BeginTransaction();

                    OleDbCommand eliminar = new OleDbCommand(sql, conex, trans);

                    eliminar.Parameters.AddWithValue("@pai_nombre", nombre);

                    eliminar.ExecuteNonQuery();

                    trans.Commit();

                    eliminado = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                eliminado = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return eliminado;
        }

        public bool ModifyPais(string nombre_anterior, string nombre)
        {
            bool modificado = false;

            OleDbConnection conex = null;
            OleDbTransaction trans = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                trans = null;

                if (null != conex)
                {

                    string sql = "UPDATE pai_paises SET pai_nombre = @pai_nombre," +
                                    "WHERE pai_nombre = @pai_nombre_anterior";

                    conex.Open();
                    trans = conex.BeginTransaction();

                    OleDbCommand modificar = new OleDbCommand(sql, conex, trans);

                    modificar.Parameters.AddWithValue("@pai_nombre", nombre);
                    modificar.Parameters.AddWithValue("@pai_nombre_anterior", nombre_anterior);

                    modificar.ExecuteNonQuery();

                    trans.Commit();

                    modificado = true;

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                modificado = false;
                trans.Rollback();
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return modificado;
        }

        public System.Data.DataTable FindAllPaises()
        {
            DataTable tablaPaises = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();
                tablaPaises = new DataTable();

                if (null != conex)
                {

                    string sql = "SELECT pai_id, pai_nombre" +
                                    " FROM pai_paises pai";

                    OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);

                    conex.Open();

                    adaptador.Fill(tablaPaises);

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                tablaPaises = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return tablaPaises;
        }

        public Gestión_UTN.Entities.Pais FindPaisByName(string nombre)
        {
            Pais pais = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT pai_id, pai_nombre" +
                                    " FROM pai_paises pai" +
                                    " WHERE pai_nombre = @pai_nombre";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@pai_nombre", nombre);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int id = Convert.ToInt32(lector[0].ToString());
                        string nom = lector[1].ToString();

                        pais = new Pais(id, nom);
                    }

                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                pais = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return pais;
        }

        public Pais FindPaisById(int id)
        {
            Pais pais = null;

            OleDbConnection conex = null;

            try
            {
                conex = util.Conexion.conectarAccess();

                if (null != conex)
                {

                    string sql = "SELECT pai_id, pai_nombre" +
                                    " FROM pai_paises pai" +
                                    " WHERE pai_id = @pai_id";

                    OleDbCommand consulta = new OleDbCommand(sql, conex);

                    conex.Open();

                    consulta.Parameters.AddWithValue("@pai_id", id);

                    OleDbDataReader lector = consulta.ExecuteReader();

                    while (lector.Read())
                    {
                        int pai_id = Convert.ToInt32(lector[0].ToString());
                        string nom = lector[1].ToString();

                        pais = new Pais(pai_id, nom);
                    }
                    conex.Close();
                }

            }
            catch (Exception ex)
            {
                pais = null;
                conex.Close();

                throw new exceptions.daexception(ex.Message);
            }

            return pais;
        }
    }
}
